import { defineStore } from 'pinia';
import { getFilterMenuParams } from '@/common/api/api';

export const useIndexStore = defineStore('userIndex', {
    state: () => ({
        tabIndex: 0,
        tabbarList: [{
            name: '首页',
            value: 'home',
            route: '/pages/tabBar/home/index',
            icon: '/static/tabbar/home_sel.png',
            activeIcon: '/static/tabbar/home_sel.png',
            badge: 0
        },{
            name: '1V1问答',
            value: 'faq',
            route: '/pages/tabBar/faq/index',
            icon: '/static/tabbar/faq_sel.png',
            activeIcon: '/static/tabbar/faq_sel.png',
            badge: 0
        },{
            name: '找房',
            value: 'find',
            route: '/pages/tabBar/find/index',
            icon: '/static/tabbar/find_sel.png',
            activeIcon: '/static/tabbar/find_sel.png',
            badge: 0
        },{
            name: '活动报名',
            value: 'active',
            route: '/pages/tabBar/active/index',
            icon: '/static/tabbar/active_sel.png',
            activeIcon: '/static/tabbar/active_sel.png',
            badge: 0
        },{
            name: '我的',
            value: 'my',
            route: '/pages/tabBar/my/index',
            icon: '/static/tabbar/my_sel.png',
            activeIcon: '/static/tabbar/my_sel.png',
            badge: 0
        }],
		newMessage: false,
		nowCityCode: "310100",
        cityList: [],
		cityMap: {},
        filterAreaList: [],
        filterMetroList: [],
        filterAmountList: [],
        filterHouseList: [],
        filterSizeList: [],
        filterTagList: [],
        tencentMetroMap: {},
        panDetail: {},
        nowAreaList: [],
        rentCityMap: {},
        fapaiCityMap: {},
        fansFormOptions: {},
        PKType: 0,
		displayAD: true,
		uploadParam: {},
        uploadParamSellerJoin: null,
        uploadDistParam: null,
		searchParams: null,
        searchRecord: [],
        colorIndex: 0,
        colorList: ['#FF0000','#00FF00','#0000FF'],
        searchPanel: [],
        adviserPubData: null,
        adviserId: '',
        adviserCardData: {},
        adviserSelectHouse: {},
        isAdviser: false,
        integralStatus: false,
        updateForum: false,
		openid: null,
		retry: 0,
		verifyToken: null,
		points: 0,
        integralInfo: null,
        POIInfo: null,
        unreadGuessMsg: {},
        disclaimer: '该小程序内容系新房超市自行采集编辑，由于信息的获取渠道和房源信息的变化，我们不能保证会及时更新到最新信息，有存在开发商合同或实际交付信息与小程序内容不相符的风险，所以该内容仅作为读者购房参考！如果由于阅读该信息，对您购房行为产生损失，新房超市不承担相关责任！如果您不认同以上声明，请立即停止浏览该页面！如果您继续阅读浏览此页面信息，则视为已知晓认可该免责声明！',
    }),

    getters: {
        currentColor: (state) => {
            return state.colorList[state.colorIndex]
        },
    },

    actions: {
        /**
         * 设置下标
         * @param {Number} index 跳转的下标
         * @param {Boolean} isTabbar 是否底部tabbar点击 true跳转
        */
        setTabIndex(index, isTabbar=false) {
            if(this.tabIndex === index) return;
            this.tabIndex = index;
            if(isTabbar) {
                uni.switchTab({
                    url: this.tabbarList[index].route
                })
            }
        },

        //设置对应下标的角标badge
        setBadgeByType({type, num}) {
            const currTabObj = this.tabbarList.find(item => item.value === type);
            if(currTabObj) {
                currTabObj.badge = num;
            }
        },

        setNewMessage(type){
			this.newMessage = type
		},

        setNowCityCode(info){
			this.nowCityCode = info;
		},
		setCityList(list){
			this.cityList = list;
		},
        //获取城市筛选菜单数据
        async getCityFilterMenuParams(){
            if(this.cityMap[this.nowCityCode]) return;
            const res = await getFilterMenuParams();
            if(res.status === 0) {
                this.setCityMap({
                    city: this.nowCityCode,
                    value: res.detail
                });
                let findCity = res.detail.city.find(item => item.value === this.nowCityCode);
                if(findCity) {
                    let areaList = findCity.area.map(item => {
                        item.name = item.label;
                        item.id = item.value;
                        item.children = item.street.map(child => {
                            return {
                                name: child,
                                id: child
                            };
                        });
                        return item;
                    });
                    let metroList = findCity.subway.map(item => {
                        item.name = item.label;
                        item.id = item.value;
                        item.color = item.color;
                        item.children = item.station.map(child => {
                            return {
                                name: child,
                                id: child
                            };
                        });
                        return item;
                    });
                    this.filterAreaList = [{name:'不限', id:''}, ...areaList];
                    this.filterMetroList = [{name:'不限', id:''}, ...metroList];
                    this.filterAmountList = res.detail.amount.map(item => {
                        item.name = item.title;
                        item.id = item.value;
                        return item;
                    });
                    this.filterHouseList = res.detail.house.map(item => {
                        item.name = item.title;
                        item.id = item.value;
                        return item;
                    });
                    this.filterSizeList = res.detail.size.map(item => {
                        item.name = item.title;
                        item.id = item.value;
                        return item;
                    });
                    this.filterTagList = res.detail.more.map(item => {
                        item.name = item.title;
                        item.id = item.value;
                        return item;
                    });
                }
            }
        },
		setCityMap(data){
			this.cityMap[data.city] = data.value;
		},
        setTencentMetroMap(data) {
            this.tencentMetroMap[data.city] = data.value
        },
        setPanDetail(info){
            this.panDetail = info
        },
        setNowAreaList(list){
            this.nowAreaList = list
        },
        setFansFormOptions(data) {
            this.fansFormOptions = data
        },
		setNewMessage(type){
			this.newMessage = type
		},
		setPKType(type){
			this.PKType = type
		},
        setIntegralStatus(type){
            this.integralStatus = true
        },

		setDisplayAD(type) {
			this.displayAD = type
		},

		setPoints(type) {
			this.points = type
		},

        setIntegralInfo(info) {
        	this.integralInfo = info
        },

        setPOIInfo(info=null) {
            this.POIInfo = info
        },

		setSearchParams(type){
			this.searchParams = type;
		},
        setSearchRecord(arr) {
            this.searchRecord = arr
        },
        setSearchPanel(arr){
            this.searchPanel = arr
        },
        setAdviserPubData(info){
            this.adviserPubData = info
        },
        setAdviserId(id) {
            this.adviserId = id
        },
        setAdviserCardData(info){
            this.adviserCardData = info
        },
        setAdviserSelectHouse(info){
            this.adviserSelectHouse = info
        },
        setIsAdviser(type){
            this.isAdviser = type
        },
		
		setUploadParam(info){
			this.uploadParam = {
                ...this.uploadParam,
                [info.paramType]: info
            };
		},
        setSellerJoinUploadParam(info){
			this.uploadParamSellerJoin = info
		},
        setEmojiDecoder(arr) {
            this.emojiDecoder = arr
        },

        setDistParam(info){
            this.uploadDistParam = info
        },

		token(token) {
			this.token = token;
		},
		retryAdd(){
			this.retry += 1
		},
		retryClear(){
			this.retry = 0
		},
		setOpenid(openid) {
			this.openid = openid
		},
		setVerifyToken(info){
			this.verifyToken = info
		},

        setUpdateForum(flag) {
            this.updateForum = flag
        },

        setUnreadGuessMsg(msg) {
            this.unreadGuessMsg = {...this.unreadGuessMsg, ...msg}
        }
    }
})